import { UserConfig, defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { VantResolver, VantImports } from "@vant/auto-import-resolver";
import { fileURLToPath } from "node:url";
import legacyPlugin from "@vitejs/plugin-legacy";
import viteCompression from "vite-plugin-compression";

// https://vitejs.dev/config/
export default defineConfig(({ command, mode, isSsrBuild, isPreview }) => {
  console.log(command, mode, isSsrBuild, isPreview);
  const env = loadEnv(mode, process.cwd(), "");
  console.log(env.VITE_BASE_PATH);
  return {
    base: env.VITE_BASE_PATH,
    build: {
      target: "es2015",
      minify: "terser",
      terserOptions: {
        compress: {
          //  生产环境时移除console
          drop_console: true,
          drop_debugger: true,
        },
      },
    },
    plugins: [
      // 浏览器兼容问题配置
      legacyPlugin({
        targets: ["defaults", "not IE 11"],
        additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
        renderLegacyChunks: true,
        polyfills: [
          "es.symbol",
          "es.promise",
          "es.promise.finally",
          "es/map",
          "es/set",
          "es.array.filter",
          "es.array.for-each",
          "es.array.flat-map",
          "es.object.define-properties",
          "es.object.define-property",
          "es.object.get-own-property-descriptor",
          "es.object.get-own-property-descriptors",
          "es.object.keys",
          "es.object.to-string",
          "web.dom-collections.for-each",
          "esnext.global-this",
          "esnext.string.match-all",
        ],
      }),
      vue(),
      AutoImport({
        imports: [VantImports()],
        resolvers: [VantResolver()],
      }),
      Components({
        resolvers: [VantResolver()],
      }),
      viteCompression({
        verbose: true, // 是否在控制台输出压缩结果
        disable: false, // 默认 false, 设置为 true 来禁用压缩
        threshold: 256, // 只处理大于此大小的资源（单位：字节）。默认值为 0。
        algorithm: "gzip", // 使用 gzip 压缩
        ext: ".gz", // 输出文件的扩展名
        deleteOriginFile: false,
      }),
      viteCompression({
        verbose: true, // 是否在控制台输出压缩结果
        disable: false, // 默认 false, 设置为 true 来禁用压缩
        threshold: 256, // 只处理大于此大小的资源（单位：字节）。默认值为 0。
        algorithm: "brotliCompress", // 使用 brotli 压缩
        ext: ".br", // 输出文件的扩展名
        deleteOriginFile: false,
      }),
    ],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    server: {
      cors: true,
      proxy: {
        "/api": {
          target: "http://127.0.0.1:8083",
          // target: 'https://www.iskxz.com',
          ws: true,
          changeOrigin: true,
        },
        "/static/": {
          target: "http://127.0.0.1:8083",
          // target: 'https://www.iskxz.com/api',
          ws: true,
          changeOrigin: true,
        },
      },
    },
  } as UserConfig;
});
